package com.ly.algorithm.DataStructures.tree.avltree;



/**
 * @Classname AVLTree
 * @Description TODO
 * @Date 2020/5/3 15:36
 * @Author 冷心影翼
 */
public class AVLTree {

    public Node root;

    public void add(Node node) {
        if(root!=null) {
            root.add(node);
        }else {
            root = node;
        }
    }

    public void infixOrder() {
        if (root!=null) {
            root.infixOrder();
        }
    }

    public Node search(int data) {
        if (root!=null) {
            return root.search(data);
        }
        return null;
    }

    public Node searchParent(int data) {
        if (root!=null) {
            return root.searchParent(data);
        }
        return null;
    }

    public void delete(int data) {
        if(root!=null) {
            if(root.left == null && root.right == null) {
                Node search = search(data);
                if(search!=null) {
                    root = null;
                }
                return;
            }
            if(root.left == null && root.right!=null) {
                Node search = search(data);
                if(search!=null) {
                    root = root.right;
                }
                return;
            }
            if(root.right == null && root.left!=null) {
                Node search = search(data);
                if(search!=null) {
                    root = root.left;
                }
                return;
            }
            if (root.right != null && root.left!=null) {
                root.delete(data);
            }
        }


    }

}

class Test {

    public static void main(String[] args) {
        int[] arr = {4,3,6,5,7,8,23,12,33,56,88,31,111,222,333,444,555};
        AVLTree avlTree = new AVLTree();
        for(int i=0;i<arr.length;i++) {
            avlTree.add(new Node(arr[i]));
        }

        avlTree.infixOrder();
        System.out.println(avlTree.root.height());
        System.out.println(avlTree.root.left.height());
        System.out.println(avlTree.root.right.height());
    }
}

class Node{
    int data;
    Node left;
    Node right;

    public Node(int data) {
        this.data = data;
    }

    public int leftHeight() {
        if(left == null) {
            return 0;
        }
        return left.height();
    }

    public int rightHeight() {
        if(right == null) {
            return 0;
        }
        return right.height();
    }

    public int height() {
        return Math.max(left == null? 0:left.height(),right == null?0:right.height())+1;
    }

//    public int t() {
//        int leftMax = 0;
//        int rightMax = 0;
//        if(left!=null) {
//            leftMax = left.t();
//        }
//        if(right!=null) {
//            rightMax = right.t();
//        }
//        return leftMax>rightMax?leftMax:rightMax +1.txt;
//    }

    @Override
    public String toString() {
        return "Node{" +
                "data=" + data +
                '}';
    }

    public void add(Node node) {
        if (node == null) {
            return;
        }
        if(node.data<data) {
            if(left == null) {
                left = node;
            }else {
                left.add(node);
            }
        }else {
            if(right == null) {
                right = node;
            }else {
                right.add(node);
            }
        }

        if(rightHeight()-leftHeight()>1) {
            if(right != null && right.leftHeight() > right.rightHeight()) {
                right.rightRote();
                leftRote();
            }else {
                leftRote();
            }
            return;
        }
        if(leftHeight()-rightHeight()>1) {
            if(left != null && left.rightHeight() > left.leftHeight()) {
                left.leftRote();
                rightRote();
            }else {
                rightRote();
            }
        }
    }

    public void infixOrder() {
        if(left!=null) {
            left.infixOrder();
        }
        System.out.println(this);
        if(right != null) {
            right.infixOrder();
        }
    }

    public Node search(int data) {
        Node res = null;
        if(data<this.data && this.left != null) {
            res =  left.search(data);
        }
        if (data == this.data) {
            res = this;
        }
        if(data>this.data && this.right!=null){
            res = right.search(data);
        }
        return res;
    }

    public Node searchParent(int data) {
        Node res = null;
        if(data<this.data && this.left != null) {
            if(this.left.data == data) {
                res = this;
            }else {
                res = left.searchParent(data);
            }
        }
        if(data>this.data && this.right!=null){
            if(this.right.data == data) {
                res =  this;
            }else {
                res = right.searchParent(data);
            }
        }
        return res;
    }

    public void delete(int data) {
        Node cur = search(data);
        Node parent = searchParent(data);
        if(cur == null) {
            return;
        }
        if(cur.left == null && cur.right == null) {
            if(parent.left!=null && parent.left.data == cur.data) {
                parent.left = null;
                return;
            }else {
                parent.right = null;
                return;
            }
        }
        if(cur.left == null && cur.right!=null) {
            if(parent.left!=null && parent.left.data == cur.data) {
                parent.left = cur.right;
            }else {
                parent.right = cur.right;
            }
        }
        if(cur.right == null && cur.left!=null) {
            if(parent.left!=null && parent.left.data == cur.data) {
                parent.left = cur.left;
            }else {
                parent.right = cur.left;
            }
        }
        if(cur.right != null && cur.left!=null) {
            Node node = cur;
            cur = cur.right;
            while (cur.left != null ) {
                cur = cur.left;
            }
            delete(cur.data);
            node.data = cur.data;
        }
    }

    public void leftRote() {
        Node node = new Node(this.data);
        node.left = left;
        node.right = right.left;
        data = right.data;
        right = right.right;
        left = node;
    }

    public void rightRote() {
        Node node = new Node(this.data);
        node.right = right;
        node.left = left.right;
        data = left.data;
        left = left.left;
        right = node;
    }
}